﻿package mortal.common.shortcutsKey
{
    import com.gengine.keyBoard.*;
    import com.mui.serialization.json.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class ShortcutsKey extends Object
    {
        private var _keyMap:Dictionary;
        private var _defaultKeyMap:Dictionary;
        public var moduleAry:Array;
        public var skillsAry:Array;
        private static var _instance:ShortcutsKey;

        public function ShortcutsKey()
        {
            this._defaultKeyMap = new Dictionary();
            this.moduleAry = [];
            this.skillsAry = [];
            if (_instance)
            {
                throw new Error("ShortcutsKey singleton ");
            }
            _instance = this;
            this.initKeyMap();
            this.initDefaultShortcutsKey();
            return;
        }// end function

        private function initKeyMap() : void
        {
            var _loc_1:int = 0;
            if (this._keyMap == null)
            {
                this._keyMap = new Dictionary();
                this.addKeyMap(ModuleType.Pack, Language.getString(20021));
                this.addKeyMap(ModuleType.Players, Language.getString(20022));
                this.addKeyMap(ModuleType.Skills, Language.getString(20052));
                this.addKeyMap(ModuleType.SmallMap, Language.getString(20023));
                this.addKeyMap(ModuleType.PickBox, Language.getString(10021));
                this.addKeyMap(ModuleType.SwitchTarget, Language.getString(20024));
                this.addKeyMap(ModuleType.Group, Language.getString(30201));
                this.addKeyMap(ModuleType.Tasks, Language.getString(20120));
                this.addKeyMap(ModuleType.Pets, Language.getString(70023));
                this.addKeyMap(ModuleType.Friend, Language.getString(40001));
                this.addKeyMap(ModuleType.ShopMall, Language.getString(30059));
                this.addKeyMap(ModuleType.Mail, Language.getString(50001));
                this.addKeyMap(ModuleType.Build, Language.getString(30006));
                this.addKeyMap(ModuleType.Mounts, Language.getString(30300));
                this.addKeyMap(ModuleType.Guild, Language.getString(60001));
                this.addKeyMap(ModuleType.Wizard, Language.getString(30400));
                this.addKeyMap(ModuleType.Custom, Language.getString(40601));
                this.addKeyMap(ModuleType.Arena, Language.getString(20335));
                this.addKeyMap(ModuleType.AutoFight, Language.getString(20336));
                this.addKeyMap(ModuleType.RangeAutoFight, Language.getString(20337), false);
                this.addKeyMap(ModuleType.DownUpMounts, Language.getString(20338));
                this.addKeyMap(ModuleType.Rest, Language.getString(20339));
                this.addKeyMap(ModuleType.XP, Language.getString(20340));
                this.addKeyMap(ModuleType.F9, Language.getString(20341), false);
                this.addKeyMap(ModuleType.Enter, Language.getString(20342), false);
                this.addKeyMap(ModuleType.SPACE, Language.getString(20343), false);
                this.addKeyMap(ModuleType.RUN, Language.getString(20344), false);
                _loc_1 = 1;
                while (_loc_1 <= 19)
                {
                    
                    if (_loc_1 <= 10)
                    {
                        this.addKeyMap(_loc_1, Language.getString(20049) + _loc_1);
                    }
                    else
                    {
                        this.addKeyMap(_loc_1, Language.getString(20050) + _loc_1);
                    }
                    _loc_1++;
                }
            }
            return;
        }// end function

        private function initDefaultShortcutsKey() : void
        {
            this.registerDefaultKeyMap(KeyCode.N1, ModuleType.Shortcuts1);
            this.registerDefaultKeyMap(KeyCode.N2, ModuleType.Shortcuts2);
            this.registerDefaultKeyMap(KeyCode.N3, ModuleType.Shortcuts3);
            this.registerDefaultKeyMap(KeyCode.N4, ModuleType.Shortcuts4);
            this.registerDefaultKeyMap(KeyCode.N5, ModuleType.Shortcuts5);
            this.registerDefaultKeyMap(KeyCode.N6, ModuleType.Shortcuts6);
            this.registerDefaultKeyMap(KeyCode.N7, ModuleType.Shortcuts7);
            this.registerDefaultKeyMap(KeyCode.N8, ModuleType.Shortcuts8);
            this.registerDefaultKeyMap(KeyCode.N9, ModuleType.Shortcuts9);
            this.registerDefaultKeyMap(KeyCode.N0, ModuleType.Shortcuts10);
            this.registerDefaultKeyMap(KeyCode.N1, ModuleType.Shortcuts11, true);
            this.registerDefaultKeyMap(KeyCode.N2, ModuleType.Shortcuts12, true);
            this.registerDefaultKeyMap(KeyCode.N3, ModuleType.Shortcuts13, true);
            this.registerDefaultKeyMap(KeyCode.N4, ModuleType.Shortcuts14, true);
            this.registerDefaultKeyMap(KeyCode.N5, ModuleType.Shortcuts15, true);
            this.registerDefaultKeyMap(KeyCode.N6, ModuleType.Shortcuts16, true);
            this.registerDefaultKeyMap(KeyCode.N7, ModuleType.Shortcuts17, true);
            this.registerDefaultKeyMap(KeyCode.N8, ModuleType.Shortcuts18, true);
            this.registerDefaultKeyMap(KeyCode.N9, ModuleType.Shortcuts19, true);
            this.registerDefaultKeyMap(KeyCode.F9, ModuleType.F9, false);
            this.registerDefaultKeyMap(KeyCode.ENTER, ModuleType.Enter, false);
            this.registerDefaultKeyMap(KeyCode.SPACE, ModuleType.SPACE, false);
            this.registerDefaultKeyMap(KeyCode.TILDE, ModuleType.RUN, false);
            this.registerDefaultKeyMap(KeyCode.B, ModuleType.Pack, false);
            this.registerDefaultKeyMap(KeyCode.C, ModuleType.Players, false);
            this.registerDefaultKeyMap(KeyCode.V, ModuleType.Skills, false);
            this.registerDefaultKeyMap(KeyCode.M, ModuleType.SmallMap, false);
            this.registerDefaultKeyMap(KeyCode.Z, ModuleType.PickBox, false);
            this.registerDefaultKeyMap(KeyCode.TAB, ModuleType.SwitchTarget, false);
            this.registerDefaultKeyMap(KeyCode.T, ModuleType.Group, false);
            this.registerDefaultKeyMap(KeyCode.Q, ModuleType.Tasks, false);
            this.registerDefaultKeyMap(KeyCode.W, ModuleType.Pets, false);
            this.registerDefaultKeyMap(KeyCode.F, ModuleType.Friend, false);
            this.registerDefaultKeyMap(KeyCode.S, ModuleType.ShopMall, false);
            this.registerDefaultKeyMap(KeyCode.E, ModuleType.Mail, false);
            this.registerDefaultKeyMap(KeyCode.K, ModuleType.Build, false);
            this.registerDefaultKeyMap(KeyCode.J, ModuleType.Mounts, false);
            this.registerDefaultKeyMap(KeyCode.G, ModuleType.Guild, false);
            this.registerDefaultKeyMap(KeyCode.P, ModuleType.Wizard, false);
            this.registerDefaultKeyMap(KeyCode.A, ModuleType.AutoFight, false);
            this.registerDefaultKeyMap(KeyCode.A, ModuleType.RangeAutoFight, true);
            this.registerDefaultKeyMap(KeyCode.H, ModuleType.DownUpMounts, false);
            this.registerDefaultKeyMap(KeyCode.D, ModuleType.Rest, false);
            this.registerDefaultKeyMap(KeyCode.X, ModuleType.XP, false);
            this.registerDefaultKeyMap(KeyCode.O, ModuleType.Custom, false);
            this.registerDefaultKeyMap(KeyCode.L, ModuleType.Arena, false);
            return;
        }// end function

        public function hasKeyCode(param1:uint) : Boolean
        {
            if (param1 >= 48 && param1 <= 90 || param1 == KeyCode.TAB)
            {
                if (param1 > 57 && param1 < 65)
                {
                    return false;
                }
                return true;
            }
            return false;
        }// end function

        private function registerDefaultKeyMap(param1:uint, param2:Object, param3:Boolean = false) : void
        {
            this._defaultKeyMap[param2] = KeyBoardManager.createKeyData(param1, param3);
            return;
        }// end function

        public function register(param1:uint, param2:Object, param3:Boolean = false) : void
        {
            var _loc_4:* = KeyBoardManager.createKeyData(param1, param3);
            var _loc_5:* = this.getKeyMap(param2);
            if (_loc_5)
            {
                _loc_4.keyMap = _loc_5;
                _loc_5.keyData = _loc_4;
            }
            else
            {
                _loc_4.keyMap = null;
            }
            return;
        }// end function

        public function getKeyMap(param1:Object) : KeyMapData
        {
            return this._keyMap[param1];
        }// end function

        public function getKeyMapData(param1:uint, param2:Boolean = false) : KeyMapData
        {
            var _loc_3:KeyMapData = null;
            for each (_loc_3 in this.moduleAry)
            {
                
                if (_loc_3.displayKeyData.isShift == param2 && _loc_3.displayKeyData.keyCode == param1)
                {
                    return _loc_3;
                }
            }
            for each (_loc_3 in this.skillsAry)
            {
                
                if (_loc_3.displayKeyData.isShift == param2 && _loc_3.displayKeyData.keyCode == param1)
                {
                    return _loc_3;
                }
            }
            return null;
        }// end function

        public function getDefaultKeyData(param1:Object) : KeyData
        {
            return this._defaultKeyMap[param1];
        }// end function

        public function setDefaultShortcutsKey() : void
        {
            var _loc_1:KeyMapData = null;
            for each (_loc_1 in this.moduleAry)
            {
                
                _loc_1.setDefault();
            }
            for each (_loc_1 in this.skillsAry)
            {
                
                _loc_1.setDefault();
            }
            return;
        }// end function

        public function addKeyMap(param1:Object, param2:String, param3:Boolean = true) : void
        {
            var _loc_4:* = new KeyMapData(param1, param2, param3);
            this._keyMap[param1] = _loc_4;
            if (param1 is int)
            {
                this.skillsAry.push(_loc_4);
            }
            else
            {
                this.moduleAry.push(_loc_4);
            }
            return;
        }// end function

        public function setServerStr(param1:String) : void
        {
            var _loc_2:Object = null;
            var _loc_3:KeyMapData = null;
            var _loc_4:Object = null;
            if (param1 == "" || param1 == null)
            {
                this.setDefaultShortcutsKey();
            }
            else
            {
                _loc_2 = JSON.deserialize(param1) as Object;
                for each (_loc_3 in this.moduleAry)
                {
                    
                    if (_loc_3.isCanEdit == false)
                    {
                        _loc_3.setDefault();
                        continue;
                    }
                    _loc_4 = _loc_2[_loc_3.key];
                    if (_loc_4)
                    {
                        this.register(_loc_4.c, _loc_3.key, _loc_4.s == 0 ? (false) : (true));
                        continue;
                    }
                    if (_loc_3.key == ModuleType.SwitchTarget)
                    {
                        _loc_3.setDefault();
                        continue;
                    }
                    _loc_3.clear();
                }
                for each (_loc_3 in this.skillsAry)
                {
                    
                    if (_loc_3.isCanEdit == false)
                    {
                        _loc_3.setDefault();
                        continue;
                    }
                    _loc_4 = _loc_2[_loc_3.key];
                    if (_loc_4)
                    {
                        this.register(_loc_4.c, _loc_3.key, _loc_4.s == 0 ? (false) : (true));
                        continue;
                    }
                    _loc_3.clear();
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutKeyUpdate, this.moduleAry.concat(this.skillsAry)));
            return;
        }// end function

        public function recoverySetter() : void
        {
            var _loc_1:KeyMapData = null;
            for each (_loc_1 in this.moduleAry)
            {
                
                _loc_1.recoveryKeyData();
            }
            for each (_loc_1 in this.skillsAry)
            {
                
                _loc_1.recoveryKeyData();
            }
            return;
        }// end function

        public function getServerStr() : String
        {
            var _loc_2:KeyMapData = null;
            var _loc_3:Object = null;
            var _loc_1:Object = {};
            for each (_loc_2 in this.moduleAry)
            {
                
                if (_loc_2.isCanEdit)
                {
                    _loc_2.updateKeyData();
                    _loc_3 = _loc_2.getServerObject();
                    if (_loc_3)
                    {
                        _loc_1[_loc_2.key] = _loc_3;
                    }
                }
            }
            for each (_loc_2 in this.skillsAry)
            {
                
                if (_loc_2.isCanEdit)
                {
                    _loc_2.updateKeyData();
                    _loc_3 = _loc_2.getServerObject();
                    if (_loc_3)
                    {
                        _loc_1[_loc_2.key] = _loc_3;
                        continue;
                    }
                    _loc_2.clear();
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutKeyUpdate, this.moduleAry.concat(this.skillsAry)));
            return JSON.serialize(_loc_1);
        }// end function

        public static function get instance() : ShortcutsKey
        {
            if (_instance == null)
            {
                _instance = new ShortcutsKey;
            }
            return _instance;
        }// end function

    }
}
